package org.infinitest.swingui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import org.infinitest.TestEvent;

class EventInfoFrame extends JDialog
{
  private JTextArea textArea;
  private JButton closeButton;

  public EventInfoFrame(TestEvent event)
  {
    setAlwaysOnTop(true);
    textArea = new JTextArea(event.getErrorAsString());
    textArea.setEditable(false);
    textArea.setColumns(80);
    textArea.setWrapStyleWord(true);
    textArea.setLineWrap(true);
    getContentPane().add(new JScrollPane(textArea));
    Container southPanel = new JPanel(new FlowLayout());
    Action disposeAction = new AbstractAction("Close")
    {
      public void actionPerformed(ActionEvent e)
      {
        dispose();
      }
    };
    closeButton = new JButton(disposeAction);
    southPanel.add(closeButton);
    getContentPane().add(southPanel, BorderLayout.SOUTH);
    String message = event.getMessage();
    if (message == null)
      message = "";
    setTitle(event.getTestName() + "." + event.getTestMethod() + "()");
    pack();
    closeButton.requestFocusInWindow();
  }

  public JButton getCloseButton()
  {
    return closeButton;
  }

  @Override
  protected JRootPane createRootPane()
  {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction()
    {
      public void actionPerformed(ActionEvent actionEvent)
      {
        setVisible(false);
      }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
  }

  public JTextArea getTextArea()
  {
    return textArea;
  }
}
